#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1000000000000000000;
int a[200005];
int c[1000005];
int n;
int m = 0;
int add =0 ;
int mx = 0;
void upd(int pos,int v){
	if(v>c[pos]+add){
		c[pos] = v-add;
		mx = max(mx,c[pos]);
	}
}

void solve(){
	
	m = 0;add = 0;mx =0 ;
	cin >> n;
	for(int i = 1;i<=n;i++)cin >> a[i];
	for(int i = 1;i<=n;i++)m = max(m,a[i]);
	c[0] = 0;
	for(int i = 1;i<=m;i++)c[i] = -inf;
	for(int i = 2;i<=n;i++){
		int aa = add;
		if(a[i]==a[i-1])add+=a[i];
		upd(a[i-1],max(mx+aa,c[a[i]]+a[i]+aa));

	}	
	cout << mx+add << endl;
}
signed main(){
	//freopen("/home/csps/color2.in","r",stdin);
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;cin >> t;
	while(t--)solve();
	return 0;
}
/*
g++ color.cpp -o qwq -std=c++14 -fsanitize=address,undefined
*/